package org.ihelpuoo.weboo.upload.action;

import javax.servlet.http.HttpServletRequest;

import org.ihelpuoo.lang.LoaderException;
import org.ihelpuoo.lang.NutException;
import org.ihelpuoo.parser.segment.CharSegment;
import org.ihelpuoo.parser.segment.Segment;
import org.ihelpuoo.util.Properties;
import org.ihelpuoo.weboo.action.http.Action;
import org.ihelpuoo.weboo.module.ActionConfig;
import org.ihelpuoo.weboo.module.ActionContext;
import org.ihelpuoo.weboo.module.ModuleContext;
import org.ihelpuoo.weboo.upload.Upload;

public class FilehomeAction implements Action
{

	private String	home;

	private void init(ModuleContext context, ActionConfig config, HttpServletRequest request)
			throws NutException, LoaderException
	{
		if (null == home)
		{
			synchronized (this)
			{
				if (null == home)
				{
					String name = config.getPropertyValue(context.getConfig().getStringTrimed(
							"FILE_POOL_NAME"));
					Properties p = context.nut().getConfig(name);
					Segment seg = new CharSegment(p.getStringTrimed("home"));
					seg.set("appRoot",request.getSession().getServletContext().getRealPath("/"));
					p.setProperty("home",seg.toString());
					home = context.nut().getFactory(p).getPropertyTrimed("home");
					home = Upload.getUrlPath(request,home);
				}
			}
		}
	}

	public void execute(ActionContext context) throws Exception
	{
		this.init(context.getModuleContext(),context.getActionConfig(),context.getRequest());
		context.getResponse().getWriter().write(home);
		context.getResponse().flushBuffer();
	}

}
